package bootstrap

import (
	"baoming/routes"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
)

func SetupRoute(router *gin.Engine) {
	RegisterGlobalMiddleWare(router)
	routes.RegisterApiRoutes(router)
	Setup404Handler(router)
}

func RegisterGlobalMiddleWare(router *gin.Engine) {
	router.Use(
		gin.Logger(),
		gin.Recovery(),
	)
}

func Setup404Handler(router *gin.Engine) {
	router.NoRoute(func(c *gin.Context) {
		acceptString := c.Request.Header.Get("Accept")
		if strings.Contains(acceptString, "text/html") {
			c.String(http.StatusNotFound, "页面返回404")
		} else {
			c.JSON(http.StatusOK, gin.H{"message": "页面404"})
		}
	})
}
